package com.kgc.community.controller;


import com.github.pagehelper.PageInfo;
import com.kgc.community.pojo.Village;
import com.kgc.community.pojo.VillageExample;
import com.kgc.community.service.VillageService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *
 */


@Controller
public class Villagecontroller {

    @Resource
    VillageService villageService;


//    @RequestMapping(value="buildingadd.do")
//    public String buildingadd(){
//        return "forward:buildingadd.do";
//    }

    @RequestMapping("buildinglst.do")
    public String device_list(Date buildTime, Date buildTime2, String describe, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, Model model){
        Integer pageSize = 5;
//        if (pageNum == null) {
//            pageNum = 1;
//        }
        Village village=new Village();
        System.out.println("buildTime"+buildTime);
        System.out.println("buildTime2"+buildTime2);
        System.out.println("describe"+describe);
        PageInfo<Village> pageInfo = villageService.selectAll(buildTime, buildTime2, describe, pageNum, pageSize);
        int count = villageService.Count(village);
        model.addAttribute("count",count);
        model.addAttribute("pageInfo",pageInfo);
//        model.addAttribute("buildTime",buildTime);
//        model.addAttribute("buildTime2",buildTime2);
//        model.addAttribute("describe",describe);
        return "buildinglst";
    }

    @RequestMapping("gobuildingadd.do")
    public String test(){
        return "buildingadd";
    }


    @RequestMapping(value="buildingadd.do",produces="text/html;charset=utf-8")
    public String add(Village village,HttpServletResponse response,HttpServletRequest request) throws Exception {


//        String name, String describes, String address, Date buildTime,String buildCompany,String propertyCompany,Integer blockCount,Integer roomCount
        PrintWriter out = response.getWriter();
//        Village village=new Village();
//        village.setName(name);
//        village.setDescribes(describes);
//        village.setAddress(address);
//        village.setBuildTime(buildTime);
//        village.setBuildCompany(buildCompany);
//        village.setPropertyCompany(propertyCompany);
//        village.setBlockCount(blockCount);
//        village.setRoomCount(roomCount);
        System.out.println(village.getAddress());
        Integer add = villageService.add(village);
        System.out.println(village.getAddress());

        if (add > 0) {
            out.write("<script>alert('成功');localtion.href='buildinglst.do';</script>");
        }
        return "buildingadd";
    }


    @RequestMapping("gobuildingadd2.do")
    public String selectById(Integer id,Model model) {
        Village village = villageService.selectId(id);
       Date buildTime = village.getBuildTime();
        String time="";
       if(buildTime!=null){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            time = format.format(buildTime);
        }
       model.addAttribute("time",time);
       model.addAttribute("village",village);
       return "buildingadd2";
    }

    @RequestMapping(value = "buildingadd2.do",produces="text/html;charset=utf-8")
    public String update(String times, Integer id, String name, String describes, String address, String buildCompany, Integer blockCount, Integer roomCount, HttpServletResponse response, ModelMap modelMap) throws  ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date time = sdf.parse(times);
        Village village=new Village(id,name,describes,address,time,buildCompany,blockCount,roomCount);
        if(villageService.update(village)>0){
            modelMap.addAttribute("modifyMsg","修改成功！");
        }else {
            modelMap.addAttribute("modifyMsg","修改失败！");
        }
        return "forward:buildinglst.do";
    }

    //全选删除方法
    @RequestMapping("deleteAll.do")
    public String deleteAll(HttpServletRequest request, Model model) {
        String[] subs = request.getParameterValues("sub");
        int sum = 0;
        int count = 0;
        for (String sub : subs) {
            Integer id = Integer.parseInt(sub);
            count = villageService.deleteVillageById(id);
            sum += count;
        }
        if (sum == subs.length & sum > 0) {
            model.addAttribute("deleteAllresult", true);
            return "forward:buildinglst.do";
        }
        model.addAttribute("deleteAllresult", false);
        return "forward:buildinglst.do";
    }

    //单个删除
    @RequestMapping("buildinglstDelAll.do")
    public String stallDelAll(HttpServletRequest request){
        Integer id = Integer.parseInt(request.getParameter("id"));
        int i = villageService.deleteVillageById(id);

        return "redirect:/buildinglst.do";
    }
}
